import { createRouter, createWebHistory } from "vue-router";
import { useAuthStore } from "@/store/auth";

const routes: any = [
  {
    path: "/",
    component: () => import("@/components/layout/index.vue"),
    redirect: () => {
      return { path: "/home" };
    },
  },
  {
    path: "/",
    component: () => import("@/components/layout/index.vue"),
    children: [
      {
        path: "home",
        name: "Home",
        component: () => import("@/views/home/index.vue"),
      },
      {
        path: "course",
        name: "Course",
        component: () => import("@/views/course/index.vue"),
        // meta: { requiresAuth: true },
      },
      {
        path: "examination",
        name: "Examination",
        component: () => import("@/views/examination/index.vue"),
        meta: { requiresAuth: true },
      },
      {
        path: "hot",
        name: "Hot",
        component: () => import("@/views/hot/index.vue"),
      },
      {
        //新闻详情
        path: "/NewsDetails",
        name: "NewsDetails",
        component: () => import("@/views/hot/NewsDetails.vue"),
      },
      {
        path: "teachers",
        name: "Teachers",
        component: () => import("@/views/teachers/index.vue"),
        meta: { requiresAuth: true },
      },
      {
        path: "correction",
        name: "Correction",
        component: () => import("@/views/correction/index.vue"),
        meta: { requiresAuth: true },
      },
      {
        // 历年真题
        path: "previousExam",
        name: "PreviousExam",
        component: () => import("@/views/previousExam/index.vue"),
        meta: { requiresAuth: true },
      },
      {
        path: "customTest",
        name: "CustomTest",
        component: () => import("@/views/customTest/index.vue"),
        meta: { requiresAuth: true },
      },
      {
        path: "custom",
        name: "Custom",
        component: () => import("@/views/customTest/custom.vue"),
        meta: { requiresAuth: true },
      },
      {
        path: "socialPractice",
        name: "SocialPractice",
        component: () => import("@/views/socialPractice/index.vue"),
        meta: { requiresAuth: true },
      },
      {
        path: "exerciseList",
        name: "ExerciseList",
        component: () => import("@/views/course/exerciseList.vue"),
        meta: { requiresAuth: true },
      },
      {
        path: "database",
        name: "Database",
        component: () => import("@/views/database/index.vue"),
        meta: { requiresAuth: true },
      },
    ],
  },
  {
    path: "/learnVideo",
    name: "LearnVideo",
    component: () => import("@/views/course/learnVideo.vue"),
    // meta: { requiresAuth: true },
  },
  {
    path: "/answer",
    name: "Answer",
    component: () => import("@/views/answer/index.vue"),
    meta: { requiresAuth: true },
  },
  {
    path: "/exercise",
    name: "Exercise",
    component: () => import("@/views/course/exercise.vue"),
    meta: { requiresAuth: true },
  },
  {
    path: "/report",
    name: "Report",
    component: () => import("@/views/report/index.vue"),
    meta: { requiresAuth: true },
  },
  {
    //登录注册
    path: "/login",
    name: "login",
    component: () => import("@/views/login/index.vue"),
  },
  {
    //登录注册
    path: "/401",
    name: "401",
    component: () => import("@/views/error/401.vue"),
  },
  {
    //登录注册
    path: "/resetPwd",
    name: "resetPwd",
    component: () => import("@/views/login/retrievePassword.vue"),
  },
];

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
  scrollBehavior(to, from, savedPosition) {
    // 始终滚动到顶部
    return { top: 0 };
  },
});

router.beforeEach((to, from, next) => {
  const authStore = useAuthStore();

  // 检查路由是否存在
  if (to.matched.length === 0) {
    // 路由不存在，跳转到首页
    next({ name: "Home" });
    return;
  }

  console.log(to.meta.requiresAuth);
  console.log(authStore.isAuthenticated);
  if (to.meta.requiresAuth && !authStore.isAuthenticated) {
    // 重定向到登录页，并保存目标路由以便登录后跳转
    next({ name: "login", query: { redirect: to.fullPath } });
  } else if (to.name === "login" && authStore.isAuthenticated) {
    // 已登录用户访问登录页，重定向到首页
    next({ name: "Home" });
  } else {
    next();
  }
});

export default router;
